package com.ruoyi.client.fxiaoke.beans.args;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.core.util.BeanUtils;

import lombok.Data;

/**
 * Crm更新对象数据参数
 * @author 官宇
 * @date 2020-05-18
 */
@Data
public class CrmUpdateArg extends BaseArg {
    private static final long serialVersionUID = -3661876864620608225L;

    /**
     * 当前操作人OpenUserID
     */
    private String currentOpenUserId = "";
    
    /**
     * 是否需要触发工作流(默认不传时为true)
     */
    private boolean triggerWorkFlow = false;
    
    /**
     * 是否返回ID列表
     */
    private boolean includeDetailIds = true;
    
    /**
     * 接口版本
     */
    private String defineType = "v2";
    
    private Map<String,Object> data = new LinkedHashMap<String, Object>();
    
    public CrmUpdateArg(String apiName) {
    	data.put("object_data", new LinkedHashMap<String,Object>());
    	this.setApiName(apiName);
    }

   
    @Data
    public static class Datas{
    	 /**
         * 对象数据
         */
        private Map<String,Object> object_data = new LinkedHashMap<>();
        
        /**
         * 子表数据
         */
        private Map<String,List<Map<String,Object>>> details = new LinkedHashMap<>();
        
    }
    
    /**
     * 获取明细对象
     * @return
     */
    @SuppressWarnings("unchecked")
	public Map<String,Object> getObject_data(){
    	return (Map<String, Object>) data.get("object_data");
    }
    
    public void setObject_data(Map<String,Object> dataMap) {
    	data.put("object_data", dataMap);
    }
    
    @SuppressWarnings("unchecked")
	public Map<String,List<Map<String,Object>>> getDetails(){
    	if(!data.containsKey("details")) {
    		data.put("details", new LinkedHashMap<String,List<Map<String,Object>>>());
    	}
    	return (Map<String, List<Map<String, Object>>>) data.get("details");
    }
    
    /**
     * 设置对象名称
     * @param apiName
     */
    public void setApiName(String apiName) {
    	this.getObject_data().put("dataObjectApiName", apiName);
    }
    
    /**
     * 获取对象名称
     * @return
     */
    public String getApiName() {
    	return this.getObject_data().getOrDefault("dataObjectApiName","").toString();
    }
    
    /**
     * 设置业务主键
     * @param dataId
     */
    public void setDataId(String dataId) {
    	this.getObject_data().put("_id", dataId);
    }
    
    /**
     * 设置字段
     * @param field	字段名称
     * @param value 字段值
     */
    public void addFiled(String field,Object value) {
    	if(BeanUtils.isEmpty(value))
    		value = "";
    	this.getObject_data().put(field, value);
    }
    
    /**
     * 获取接口字段
     * @param field
     * @return
     */
    public Object getField(String field) {
    	return this.getObject_data().get(field);
    }
    
    /**
     * 设置关联字段主键
     * @param field	关联字段名称
     * @param value	关联字段值
     */
    @SuppressWarnings("unchecked")
	public void addRelation(String field,Object value) {
    	List<Object> values = new ArrayList<>();
    	if(this.getObject_data().containsKey(field))
    		values = (List<Object>) this.getObject_data().get(field);
    	
    	values.add(value);
    	this.getObject_data().put(field, values);
    }
    
    /**
     * 添加子表数据
     * @param subFieldName	子表字段
     * @param field			子表字段名称
     * @param value			子表添加对象值
     */
	public void addSubData(String subFieldName,Map<String,Object> value) {
    	List<Map<String,Object>> subDataList = new ArrayList<>();
    	if(this.getDetails().containsKey(subFieldName))
    		subDataList = this.getDetails().get(subFieldName);
    	
    	subDataList.add(value);
    	this.getDetails().put(subFieldName, subDataList);
    }
    
    /**
     * 设置子表
     * @param subFieldName  子表字段
     * @param subDataList	子表数据
     */
    public void setSubData(String subFieldName,List<Map<String,Object>> subDataList) {
    	this.getDetails().put(subFieldName, subDataList);
    }
}
